import 'package:flutter/material.dart';

import '../common/const/app_color_const.dart';
import '../common/const/app_style_const.dart';
import 'ripple_container.dart';

class FormItem extends StatelessWidget {
  final String label;
  final EdgeInsets padding;
  final TextStyle labelStyle;
  final bool labelSoftWrap;
  final double labelWidth;
  final TextAlign labelTextAlign;
  final TextOverflow? labelOverflow;
  final EdgeInsets? labelPadding;
  final MainAxisAlignment mainAxisAlignment;
  final CrossAxisAlignment crossAxisAlignment;
  final String value;
  final TextStyle valueStyle;
  final double valueWidth;
  final TextAlign valueTextAlign;
  final TextOverflow? valueOverflow;
  final bool valueSoftWrap;
  final EdgeInsets? valuePadding;
  final StrutStyle strutStyle;

  final WidgetBuilder? labelBuilder;
  final WidgetBuilder? valueBuilder;
  final GestureTapCallback? onTap;

  const FormItem(
      {Key? key,
      this.labelTextAlign = TextAlign.left,
      this.labelWidth = 0,
      this.labelSoftWrap = true,
      this.labelBuilder,
      this.labelOverflow,
      this.labelPadding,
      this.valuePadding,
      this.valueBuilder,
      this.valueWidth = 0,
      this.valueOverflow,
      this.valueSoftWrap = true,
      this.valueTextAlign = TextAlign.left,
      this.onTap,
      this.label = '',
      this.value = '',
      this.strutStyle = formItemStrutStyle,
      this.mainAxisAlignment = MainAxisAlignment.start,
      this.crossAxisAlignment = CrossAxisAlignment.start,
      this.padding = const EdgeInsets.only(bottom: 6.0, top: 6.0),
      this.labelStyle = formItemLabelStyle,
      this.valueStyle = formItemValueStyle})
      : super(key: key);

  Widget buildValueText() {
    var text = Text(
      value,
      style: valueStyle,
      overflow: valueOverflow,
      softWrap: valueSoftWrap,
      strutStyle: strutStyle,
      textAlign: valueTextAlign,
    );
    if (valueWidth > 0) {
      return Container(
        width: valueWidth,
        padding: valuePadding ?? const EdgeInsets.all(0),
        child: text,
      );
    } else {
      return Container(
        padding: valuePadding ?? const EdgeInsets.all(0),
        child: text,
      );
    }
  }

  Widget buildTitleText() {
    var text = Text(
      '$label：',
      textAlign: labelTextAlign,
      style: labelStyle,
      strutStyle: strutStyle,
      softWrap: labelSoftWrap,
      overflow: labelOverflow,
    );
    if (labelWidth > 0) {
      return Container(
        padding: labelPadding ?? const EdgeInsets.all(0),
        width: labelWidth,
        child: text,
      );
    } else {
      return Container(
        padding: labelPadding ?? const EdgeInsets.all(0),
        child: text,
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: padding,
      child: RippleContainer(
        onTap: onTap,
        child: Row(
            mainAxisAlignment: mainAxisAlignment,
            crossAxisAlignment: crossAxisAlignment,
            children: [
              if (null != labelBuilder) labelBuilder!(context),
              if (null == labelBuilder) buildTitleText(),
              if (null == valueBuilder) buildValueText(),
              if (null != valueBuilder) valueBuilder!(context),
            ]),
      ),
    );
  }
}
